﻿using MediatR;
using SystemLYH.API.Application.Command.LocationCommand;
using SystemLYH.Domain;
using SystemLYH.Infrstructre.Interface;

namespace SystemLYH.API.Application.Handlers.LocationHandlers
{
    /// <summary>
    /// 下拉框
    /// </summary>
    public class LocationSelectShowcommandHandler : IRequestHandler<LocationSelectShowcommand, List<Location>>
    {
        ILocationRepository _locationRepository;
        IReservoirRepository _reservoirRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="locationRepository"></param>
        /// <param name="reservoirRepository"></param>
        public LocationSelectShowcommandHandler(ILocationRepository locationRepository, IReservoirRepository reservoirRepository)
        {
            _locationRepository=locationRepository;
            _reservoirRepository=reservoirRepository;

        }

        public async Task<List<Location>> Handle(LocationSelectShowcommand request, CancellationToken cancellationToken)
        {
            var list = (from a in await _locationRepository.GetAllAsync()
                        join b in await _reservoirRepository.GetAllAsync()
                        on a.ReservoirId equals b.ReservoirName
                        where request.ReservoirId == a.ReservoirId
                        select new Location
                        {
                            LocationId= a.LocationId,
                            LocationName = a.LocationName,
                        });
            return list.ToList();
        }
    }
}
